//
//  AdoptPigViewController.m
//  PigMarket
//
//  Created by Felix on 2019/3/4.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "AdoptPigViewController.h"
#import "SelectTribeViewController.h"
#import "RegisterViewController.h"
#import "AdoptPigsCell.h"
#import "AdoptPigCell.h"
#import "AdoptHeaderView.h"
#import "DHGuidePageHUD.h"
#import "RegistGiftBox.h"

#import "AdoptTribalSelectedViewController.h"

@interface AdoptPigViewController ()
@property (nonatomic, strong) AdoptHeaderView *headerView;
@property (nonatomic, strong) NSArray *bannerArr;
@property (nonatomic, strong) NSArray *varietyDataArr;
@property (nonatomic, strong) NSNumber *memberId;
@property (nonatomic, strong) UIView *header;
@end

@implementation AdoptPigViewController

#pragma mark - init method

- (void)setupUI
{
    [self.view addSubview:self.headerView];
    [self.baseNavView setHidden:YES];
    
    // 设置APP引导页
    if (![[NSUserDefaults standardUserDefaults] boolForKey:BOOLFORKEY]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:BOOLFORKEY];
        // 静态引导页
        [self setStaticGuidePage];
        // 动态引导页
        //[self setDynamicGuidePage];
        // 视频引导页
        //[self setVideoGuidePage];
    }
}


- (void)setupLayout
{
    self.reloadBtn.mj_y += 30;
    self.reloadLab.mj_y += 30;

    self.tableView.mj_y = self.headerView.bottom;
    self.tableView.height = KScreenHeight - self.headerView.bottom - TJ_TabBarHeight;
}


- (void)initData {

    self.memberId = [UserInfoDataProvider sharedInstance].userInfo.memberId;
    [self requestBanner];
    [self requestMessage];
    [self requestPigVariety];
}

//banner请求
- (void)requestBanner {
    @weakify(self);
    [[[PigMarketApi sharedInstance] getAdoptBanner] subscribeNext:^(MessageList * _Nullable messageList) {
        @strongify(self);
        NSMutableArray *aims = [NSMutableArray array];
        for (Message *model in messageList.list) {
            [aims addObject:model.imgName];
        }
        self.bannerArr = [aims copy];
        self.headerView.hidden = NO;
        self.headerView.cycleView.imageURLStringsGroup = self.bannerArr;
    }];
}


//消息请求
- (void)requestMessage {
    
    NSNumber *memberId = nil;
    if ([JTool iSNull:self.memberId]) {
        memberId = @-1;
    }else {
        memberId = self.memberId;
    }
    @weakify(self);
    [[[PigMarketApi sharedInstance] adoptMessageWithMemberId:memberId] subscribeNext:^(MessageList * _Nullable model) {
        @strongify(self);
        if ([model.total integerValue] >0 && [model.counts integerValue] >0) {
            self.headerView.ticketType = TicketType_Have;
            NSString *text = [NSString stringWithFormat:@"您已拥有%@张共计%@元豚券，赶快用掉它", model.total, model.counts];
            self.headerView.couponsStr = text;
        }
    }];
}


//品种请求
- (void)requestPigVariety {
    @weakify(self);
    [[[PigMarketApi sharedInstance] getPigVariety] subscribeNext:^(PigVarietyList * _Nullable pigVarietyList) {
        @strongify(self);
        
        self.varietyDataArr = pigVarietyList.list;
        
        if (self.isRefresh) {
            [self endRefresh];
        }
        
        if (pigVarietyList.list.count >0) {
            self.tableView.hidden = NO;
            [self.tableView reloadData];
        }else{
            [self loadNoData];
        }

    } error:^(NSError * _Nullable error) {
        @strongify(self);
        [self loadErrorData];
    }];
}


//下拉刷新
- (void)refresh
{
    [super refresh];
    [self resetData];
    [self initData];
}


#pragma mark - delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.varietyDataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [[UIView alloc] init];
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AdoptPigsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AdoptPigsCell"];
    if (self.varietyDataArr.count > 0 && self.varietyDataArr.count > indexPath.section) {
        cell.varietyModel = self.varietyDataArr[indexPath.section];
    }
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath {

    if (self.varietyDataArr.count == 0) {
        [self toastWithContent:@"网络异常"];
        return;
    }
    [self pushToViewController:^{
        PigVariety *varietyModel = self.varietyDataArr[indexPath.section];
        AdoptTribalSelectedViewController *selectTrideVC = [[AdoptTribalSelectedViewController alloc] init];
        selectTrideVC.varietyId = varietyModel.varietiesId;
        [self.navigationController pushViewController:selectTrideVC animated:YES];
    }];
}


#pragma mark - getter and setter

- (void)registerTableViewCell
{
    self.tableView.backgroundColor = KWhiteColor;
    self.tableView.rowHeight = 116;
    [self.tableView registerClass:[AdoptPigsCell class] forCellReuseIdentifier:@"AdoptPigsCell"];
    self.tableView.tableHeaderView = self.header;
    self.tableView.mj_footer.hidden = YES;
    self.tableView.hidden = YES;
}

- (UIView *)header{
    
    if (!_header) {
        _header = [UIView new];
        _header.frame = CGRectMake(0, 0, KScreenWidth, 54);
        _header.backgroundColor = KWhiteColor;
        
        UILabel *label = [UILabel makeLabelWithText:@"小猪领养" textColor:UIColorFromRGB(0x333333) font:mFont(16) textAliment:0];
        label.frame = CGRectMake(15, 0, 100, 54);
        [_header addSubview:label];
        
        
        UILabel *detailLab = [UILabel new];
        NSMutableAttributedString *atttibutedStr = [@"领养协议" createAttributingByIcon:nil downIndexImage:@"meat_cart_enter" textColor:UIColorFromRGB(0x999999) font:mFont(12)];
        detailLab.frame = CGRectMake(15, 0, 100, 54);
        detailLab.attributedText = atttibutedStr;
        [detailLab sizeToFit];
        detailLab.mj_y = (54 - detailLab.height)/2.0;
        detailLab.mj_x = KScreenWidth - detailLab.width - 15;
        [_header addSubview:detailLab];
        
        @weakify(self);
        _header.tapBlock = ^{
            @strongify(self);
            [self showHudWithMsg:@"领养协议"];
        };
        
    }
    return _header;
}


- (AdoptHeaderView *)headerView {
    
    if (!_headerView) {
        _headerView = [[AdoptHeaderView alloc] init];
        _headerView.backgroundColor = KWhiteColor;
        _headerView.hidden = YES;
        
        @weakify(self);
        _headerView.dealRegistBlock = ^{
            @strongify(self);
            RegisterViewController *vc = [[RegisterViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
            
//            RegistGiftBox *box = [[RegistGiftBox alloc] init];
//            box.dealOkBlock = ^{
////                SeparatePlanViewController *vc = [[SeparatePlanViewController alloc] init];
////                [self.navigationController pushViewController:vc animated:YES];
//            };
//            [box showView];
        };
    }
    return _headerView;
}


- (NSArray *)bannerArr {
    if (!_bannerArr) {
        _bannerArr = [[NSArray alloc] init];
    }
    return _bannerArr;
}


- (NSArray *)varietyDataArr {
    if (!_varietyDataArr) {
        _varietyDataArr = [[NSArray alloc] init];
    }
    return _varietyDataArr;
}

#pragma mark override methods

- (void)loadErrorData
{
    [super loadErrorData];
    self.headerView.hidden = YES;
}

- (void)loadNoData
{
    [super loadNoData];
    self.headerView.hidden = YES;
}


- (void)endLoadMore{
    [super endLoadMore];
    self.headerView.hidden = NO;
}

- (void)endRefresh{
    [super endRefresh];
    self.headerView.hidden = NO;
    
}

- (void)endLoadMoreMoData
{
    [super endLoadMoreMoData];
    self.headerView.hidden = NO;
}


#pragma mark - 设置APP静态图片引导页
- (void)setStaticGuidePage {
    //NSArray *imageNameArray = @[@"guideImage1.jpg",@"guideImage2.jpg",@"guideImage3.jpg",@"guideImage4.jpg",@"guideImage5.jpg"];
    NSArray *imageNameArray = @[@"guideImage1.png",@"guideImage2.png",@"guideImage3.png",@"guideImage4.png"];
    
    DHGuidePageHUD *guidePage = [[DHGuidePageHUD alloc] dh_initWithFrame:self.view.frame imageNameArray:imageNameArray buttonIsHidden:NO];
    guidePage.slideInto = YES;
    [self.navigationController.view addSubview:guidePage];
}

#pragma mark - 设置APP动态图片引导页
- (void)setDynamicGuidePage {
    NSArray *imageNameArray = @[@"guideImage6.gif",@"guideImage7.gif",@"guideImage8.gif"];
    DHGuidePageHUD *guidePage = [[DHGuidePageHUD alloc] dh_initWithFrame:self.view.frame imageNameArray:imageNameArray buttonIsHidden:YES];
    guidePage.slideInto = YES;
    [self.navigationController.view addSubview:guidePage];
}

#pragma mark - 设置APP视频引导页
- (void)setVideoGuidePage {
    NSURL *videoURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"guideMovie1" ofType:@"mov"]];
    DHGuidePageHUD *guidePage = [[DHGuidePageHUD alloc] dh_initWithFrame:self.view.bounds videoURL:videoURL];
    [self.navigationController.view addSubview:guidePage];
}


@end
